package com.guilin.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.guilin.common.exception.ServiceException;
import com.guilin.common.utils.SecurityUtils;
import com.guilin.domain.Alumni;
import com.guilin.system.service.AlumniService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.guilin.common.core.controller.BaseController;
import com.guilin.common.core.domain.AjaxResult;
import com.guilin.domain.AlumniVisitApplication;
import com.guilin.system.service.AlumniVisitApplicationService;
import com.guilin.common.utils.poi.ExcelUtil;
import com.guilin.common.core.page.TableDataInfo;

/**
 * 校友回访申请Controller
 * 
 * @author xiaoqi
 * @date 2025-06-23
 */
@RestController
@RequestMapping("/admin-management/application")
public class AlumniVisitApplicationController extends BaseController
{
    @Autowired
    private AlumniVisitApplicationService alumniVisitApplicationService;

    @Autowired
    private AlumniService alumniService;

    /**
     * 查询校友回访申请列表
     */
    @PreAuthorize("@ss.hasPermi('admin-management:application:list')")
    @GetMapping("/list")
    public TableDataInfo list(AlumniVisitApplication alumniVisitApplication)
    {
        startPage();
        List<AlumniVisitApplication> list = alumniVisitApplicationService.selectAlumniVisitApplicationList(alumniVisitApplication);
        return getDataTable(list);
    }

    /**
     * 导出校友回访申请列表
     */
    @PreAuthorize("@ss.hasPermi('admin-management:application:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, AlumniVisitApplication alumniVisitApplication)
    {
        List<AlumniVisitApplication> list = alumniVisitApplicationService.selectAlumniVisitApplicationList(alumniVisitApplication);
        ExcelUtil<AlumniVisitApplication> util = new ExcelUtil<AlumniVisitApplication>(AlumniVisitApplication.class);
        util.exportExcel(response, list, "校友回访申请数据");
    }

    /**
     * 获取校友回访申请详细信息
     */
    @PreAuthorize("@ss.hasPermi('admin-management:application:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(alumniVisitApplicationService.selectAlumniVisitApplicationById(id));
    }

    /**
     * 新增校友回访申请
     */
    @PreAuthorize("@ss.hasPermi('admin-management:application:add')")
    @PostMapping
    public AjaxResult add(@RequestBody AlumniVisitApplication alumniVisitApplication)
    {
        return toAjax(alumniVisitApplicationService.insertAlumniVisitApplication(alumniVisitApplication));
    }

    /**
     * 修改校友回访申请
     */
    @PreAuthorize("@ss.hasPermi('admin-management:application:edit')")
    @PutMapping
    public AjaxResult edit(@RequestBody AlumniVisitApplication alumniVisitApplication)
    {
        return toAjax(alumniVisitApplicationService.updateAlumniVisitApplication(alumniVisitApplication));
    }

    /**
     * 删除校友回访申请
     */
    @PreAuthorize("@ss.hasPermi('admin-management:application:remove')")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(alumniVisitApplicationService.deleteAlumniVisitApplicationByIds(ids));
    }

    /**
     * 审核校友回访申请
     */
    @PreAuthorize("@ss.hasPermi('admin-management:application:review')")
    @PostMapping("/review")
    public AjaxResult review(@RequestBody AlumniVisitApplication application)
    {
        return toAjax(alumniVisitApplicationService.reviewAlumniVisitApplication(application));
    }


    /**
     * 校友查询自己的回访申请记录
     */
    @PreAuthorize("@ss.hasPermi('admin-management:application:list')")
    @GetMapping("/myApplications")
    public TableDataInfo alumniList()
    {
        // 获取当前登录用户
        Long userId = SecurityUtils.getLoginUser().getUserId();

        // 查找当前用户的校友信息
        Alumni alumni = alumniService.selectUserId(userId);

        // 检查校友信息是否存在
        if (alumni == null) {
            throw new ServiceException("校友信息不存在");
        }

        // 获取校友ID
        Long alumniId = alumni.getId();

        startPage();

        List<AlumniVisitApplication> list = alumniVisitApplicationService.selectPersonalApplications(alumniId);
        return getDataTable(list);
    }

}
